// Reviewer.js
import React, { useState, useEffect } from 'react';
import { useParams, Link } from 'react-router-dom';
import Web3 from 'web3';
import MaterialListABI from '../contract/ABIs/MaterialList.json'; // 材料列表合约 ABI
import ReviewerListABI from '../contract/ABIs/ReviewerList.json'; // 审核员列表合约 ABI
import ReviewerABI from '../contract/ABIs/Reviewer.json'; // 审核员合约 ABI


import ActivityListAddress from '../contract/ADDRESSes/ActivityListAddress.js'; // 活动列表合约地址
import MaterialListAddress from '../contract/ADDRESSes/MaterialListAddress'; // 材料列表合约地址
import ReviewerListAddress from '../contract/ADDRESSes/ReviewerListAddress'; // 审核员列表合约地址
import ReviewerNavBar from './nav/ReviewerNavBar.js';


// import '../static/css/app.css'; // 导入 CSS 文件
import $ from 'jquery';
// import '../static/indexcss/aacsss.css';
import gb from '../static/bootstrap/images/gb.svg';
import us from '../static/bootstrap/images/us.svg';
import user3 from '../static/bootstrap/images/user3.png';
import Head from './nav/Head.js';



const Reviewer = () => {
    // 使用 useParams 获取路由参数中的地址信息
    const { address } = useParams();
    const [web3, setWeb3] = useState(null);
    const [contractReviewerList, setcontractReviewerList] = useState(null);
    const [contractReviewer, setcontractReviewer] = useState(null);
    const [isReviewer, setisReviewer] = useState(false);

    const [contractMaterialList, setcontractMaterialList] = useState(null);
    const [materialId, setMaterialId] = useState('');
    const [userAddress, setUserAddress] = useState('');
    const [activityId, setActivityId] = useState('');
    const [contribution, setContribution] = useState('');
    useEffect(() => {
        // 重置字体大小为默认值（例如16px）  
        $("html").css({ fontSize: '16px' });
    }, []);
    useEffect(() => {
        async function init() {
            // 连接到以太坊网络
            if (window.ethereum) {
                const web3Instance = new Web3(window.ethereum);
                try {
                    // 请求用户授权
                    await window.ethereum.enable();
                    setWeb3(web3Instance);


                    // 创建材料列表合约实例
                    const materialListInstance = new web3Instance.eth.Contract(
                        MaterialListABI,
                        MaterialListAddress
                    );
                    setcontractMaterialList(materialListInstance);
                    // 创建审核员合约实例
                    const reviewerInstance = new web3Instance.eth.Contract(
                        ReviewerABI,
                        address
                    );
                    setcontractReviewer(reviewerInstance);

                    // 创建审核员列表合约实例
                    const reviewerListInstance = new web3Instance.eth.Contract(
                        ReviewerListABI,
                        ReviewerListAddress
                    );
                    setcontractReviewerList(reviewerListInstance);
                    // 获取当前用户的以太坊账户地址
                    const accounts = await web3Instance.eth.getAccounts();
                    const currentAddress = accounts[0];
                    // 获取用户合约的 owner 地址
                    const ownerAddress = await reviewerInstance.methods.getOwner().call();
                    // 检查当前账户是否是用户本人
                    setisReviewer(currentAddress === ownerAddress);
                } catch (error) {
                    console.error(error);
                }
            } else {
                console.error('请安装以太坊浏览器插件，如 MetaMask');
            }
        }
        init();
    }, [address]);

    // 审核员审核材料函数
    const RevieMaterial = async () => {
        if (contractMaterialList) {
            try {
                // 获取当前用户的以太坊账户地址
                const accounts = await web3.eth.getAccounts();
                const fromAddress = accounts[0];
                console.log("fromAddress:", fromAddress, "Input", materialId, activityId, userAddress, contribution);
                await contractMaterialList.methods.reviewMaterial(materialId, ReviewerListAddress,
                    address, userAddress, ActivityListAddress, activityId, contribution).send({ from: fromAddress });
                alert("审核完成！")
            } catch (error) {
                console.error(error);
                alert('审核失败！');
            }
        }
    };

    return (
        <div>
            {/* 如果当前账户不是审核员，则显示提示信息 */}
            {!isReviewer && (<div>您没有权限访问此页面。</div>)}

            {/* 审核员界面 */}
            {isReviewer && (
                <div className="page-wrapper" style={{
                    backgroundColor: '#FFFFFF'
                }}>
                    <div className="main-container">
                        <ReviewerNavBar address={address} />

                        <div className="app-container">
                            <Head></Head>
                            <div className="app-body">
                                <div className="container-fluid">
                                    <div>
                                        <main className="page hire-me-page">
                                            <section className="portfolio-block hire-me">
                                                <div className="container">
                                                    <div className="border rounded border-0 shadow-lg p-3 p-md-5"
                                                        data-bs-theme="light"
                                                        style={{
                                                            backgroundColor: 'rgba(36, 36, 41, 0.6)'
                                                        }}
                                                    >
                                                        <div className="container">
                                                            <h1>欢迎来到审核员端</h1>

                                                        </div>
                                                    </div>
                                                </div>
                                            </section>
                                        </main>
                                    </div>


                                </div>
                            </div>
                            <div className="app-footer">
                                <span>
                                    © Bootstrap Gallery 2023
                                </span>
                            </div>
                        </div>
                    </div>

                </div>
            )}
        </div>
    );
}

export default Reviewer;
